<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('基金分配') }}</span>
    </div>
    <!--查询条件-->
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('分配批次')" prop="distributionPeriod">
        <el-input
          v-model="queryParams.distributionPeriod"
          :placeholder="$t('分配批次')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('分配日期')" prop="distributionDate">
        <DatePicker v-model="distributionDate" type="daterange" />
      </el-form-item>
      <el-form-item :label="$t('收益主体')" prop="incomeSubject">
        <el-select v-model="queryParams.incomeSubject" value="" :placeholder="$t('承担主体')" clearable>
          <el-option
            v-for="investment in incomeSubjects"
            :key="investment.id"
            :label="investment.investorName"
            :value="investment.id"
          />
        </el-select>
      </el-form-item>
      <!--查询按钮-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>
    <!--操作按钮-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['bf:bfDistribution:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
    </el-row>
    <!--表单字段-->
    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealBfDistributionList" border>
      <el-table-column :label="$t('分配批次')" align="center" prop="distributionPeriod">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.distributionPeriod }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('分配日期')" align="center" prop="distributionDate" />
      <el-table-column :label="$t('收益主体')" align="center" prop="incomeSubjectName" />
      <!--
      <el-table-column :label="$t('分配本金') + $t('金额单位')" align="center" prop="distributionPrincipal">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.distributionPrincipal, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('分配收益') + $t('金额单位')" align="center" prop="distributionIncome">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.distributionIncome, 'w') }}</span>
        </template>
      </el-table-column>
      -->
      <el-table-column :label="$t('分配类型')" align="center" prop="distributionType" :formatter="formFormatType" />
      <el-table-column :label="$t('分配金额') + $t('金额单位')" align="center" prop="distributionAmount">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.distributionAmount, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">

        <template slot-scope="scope">
          <el-button
            v-hasPermi="['bf:bfDistribution:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}
          </el-button>
          <el-button
            v-hasPermi="['bf:bfDistribution:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基金分配对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealBfDistributionForm ref="dealBfDistributionForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  delDealBfDistribution,
  exportDealBfDistribution,
  listDealBfDistribution
} from '@/api/deal/bf/distribution/dealBfDistribution'
import dealBfDistributionForm from './dealBfDistributionForm.vue'
import { listDealInvestment } from '@/api/deal/investment/dealInvestment'

export default {
  components: {
    dealBfDistributionForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 基金分配表格数据
      dealBfDistributionList: [],
      // 弹出层标题
      title: '基金分配',
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        dealId: this.$route.query.id,
        distributionPeriod: undefined,
        distributionDate: undefined,
        incomeSubject: undefined,
        distributionType: undefined,
        distributionAmount: undefined,
        distributionPrincipal: undefined,
        distributionIncome: undefined,
        currency: undefined,
        reasonsForFundRecovery: undefined,
        earningsReason: undefined,
        assignmentReason: undefined,
        note: undefined,
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 收益主体
      incomeSubjects: [],
      // 时间区间参数
      distributionDate: [],
      // 表单校验
      rules: {},
      dictDistributionType: []
    }
  },
  created() {
    this.$nextTick(() => {
      // 获取收益主体
      this.getlistDealInvestment()
      this.getDicts('distribution_type').then(response => {
        this.dictDistributionType = response.data
      })
      // 获取列表
      this.getList()
    })
  },
  methods: {
    // 查询基金分配列表
    getList() {
      this.loading = true
      if (this.distributionDate != null && this.distributionDate.length > 1) {
        this.queryParams.startDate = this.distributionDate[0]
        this.queryParams.endDate = this.distributionDate[1]
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
      listDealBfDistribution(this.queryParams).then(response => {
        this.dealBfDistributionList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: undefined,
        distributionPeriod: undefined,
        distributionDate: undefined,
        incomeSubject: undefined,
        distributionType: undefined,
        distributionAmount: undefined,
        distributionPrincipal: undefined,
        distributionIncome: undefined,
        currency: undefined,
        reasonsForFundRecovery: undefined,
        earningsReason: undefined,
        assignmentReason: undefined,
        note: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    formFormatType(row) {
      const find = this.dictDistributionType.find(obj => obj.id === row.distributionType)
      if (find !== undefined) {
        return find.dictLabel
      }
      return ''
    },
    // 搜索按钮操作
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm('queryForm')
      this.distributionDate = []
      this.queryHandle()
    },
    // 新增按钮操作
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.dealBfDistributionForm.init(null)
      })
    },
    // 修改按钮操作
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealBfDistributionForm.init(row)
      })
    },
    // 删除按钮操作
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delDealBfDistribution(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    // 导出按钮操作
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return exportDealBfDistribution(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    },
    // 收益主体
    getlistDealInvestment() {
      this.investorId = undefined
      listDealInvestment({ 'dealId': this.$route.query.id }).then(response => {
        this.incomeSubjects = response.rows
      })
    }
  }
}
</script>
